import request from "/utils/omsRequest";
import { bus, moment } from "/utils/core";
import { showToast } from "/utils/common";
import validator from "/utils/validators";
import { payType } from "/utils/dictionary";
const app = getApp()

let arr = []
for (const key in payType) {
  arr.push({
    label: payType[key],
    value: key
  })
}

Component({
  mixins: [],
  data: {
    payType,
    payTypes: arr,
    history: {
      agree: [],
      review: [],
      refuse: []
    },
    tabKey: null,
    payTypeIndex: null,
    form: {
      depositDate: null,
      depositType: null,
      amount: null,
      sysEnclosureList: []
    }
  },
  props: {
    show: false,
    currentItem: null,
    onClose: () => { },
  },
  didMount() {
    bus.subscribe('queryDepositRecords', (contract) => {
      this.setData({
        "form.sysEnclosureList": []
      })
      dd.showLoading({
        content: '加载中...'
      })
      request({
        url: "/ajax/contractDepositRecord/depositRecordList",
        data: {
          contractId: contract.id
        }
      }).then(res => {
        dd.hideLoading()
        this.setData({
          waitingAmount: res.udf1,
          history: {
            agree: res.voList.filter(item => item.approvalStatus === 'AGREE'),
            review: res.voList.filter(item => item.approvalStatus === 'REVIEW'),
            refuse: res.voList.filter(item => item.approvalStatus === 'REFUSE')
          }
        })
        this.setData({
          tabKey: this.data.history.agree.length > 0 ? 'agree' : this.data.history.review.length > 0 ? 'review' : 'refuse'
        })
      })
    })
    bus.subscribe('depositFile', (files) => {
      if (files.length > 0 && this.props.show) {
        this.data.form.sysEnclosureList.push(...files)
        this.setData({
          "form.sysEnclosureList": this.data.form.sysEnclosureList
        });
      }
    })
  },
  didUpdate() { },
  didUnmount() {
    bus.remove("depositFile")
    bus.remove('queryDepositRecords')
  },
  methods: {
    catchMethod() { },
    // 提交form
    onFormSubmit(e) {
      Object.assign(this.data.form, e.detail.value);
      this.setData({
        "form.contractId": this.props.currentItem.product.id,
        "form.amount": parseFloat(this.data.form.amount)
      })
      let validate = new validator();
      validate.add("checkEmpty", this.data.form.depositDate, "请选择收款日期");
      validate.add("checkEmpty", this.data.form.depositType, "请选择收款方式");
      validate.add("checkIntNotZero", this.data.form.amount);
      validate.add("checkEmptyArray", this.data.form.sysEnclosureList, "请上传收款凭证");
      validate.add("checkEmpty", this.data.form.contractId, "合同id不能为空");
      if (validate.check()) {
        dd.showLoading({
          content: "加载中..."
        })
        request({
          url: "/ajax/contractDepositRecord/createContractDepositRecord",
          method: "post",
          headers: {
            "Content-Type": "application/json"
          },
          data: this.data.form
        }).then(() => {
          dd.hideLoading()
          this.hideModal({ target: { dataset: { show: false, init: true } } })
        })
      }
    },
    // 关闭modal
    hideModal(e) {
      this.setData({
        history: null,
        tabKey: null,
        payTypeIndex: null,
        form: {
          depositDate: null,
          depositType: null,
          amount: null,
          sysEnclosureList: []
        }
      })
      this.props.onClose(e)
    },
    // 收款方式change
    onPayTypeChange(e) {
      this.setData({
        payTypeIndex: e.detail.value,
        "form.depositType": this.data.payTypes[e.detail.value].value
      })
    },
    // tab切换
    onTabSelect(e) {
      this.setData({
        tabKey: e.target.dataset.key
      })
    },
    // 显示datepicker
    showDatePicker() {
      dd.datePicker({
        format: 'yyyy-MM-dd',
        currentDate: moment().format('YYYY-MM-DD'),
        success: (res) => {
          if (res.date) {
            this.setData({
              "form.depositDate": res.date
            })
          }
        },
      });
    },
    onUpload() {
      if (app.globalData.systemInfo.platform === "iOS") {
        showToast({ msg: "iOS暂不支持上传附件！请至PC端上传" })
      } else {
        dd.navigateTo({
          url: "/pages/upload/upload?accept=.jpg,.png,.jpeg,.pdf&key=depositFile&max=1"
        });
      }
    },
    onRemoveFile(e) {
      dd.confirm({
        title: '提示',
        content: '您确定要删除吗？',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        success: (result) => {
          if (result.confirm) {
            this.data.form.sysEnclosureList.splice(e.target.dataset.index, 1)
            this.setData({
              "form.sysEnclosureList": this.data.form.sysEnclosureList
            })
          }
        },
      });
    }
  },
});
